<template>
  <div>
    <TableSearch :query="query" :options="searchOpt" :search="handleSearch" />
    <div class="container">
      <TableCustom :columns="columns" :tableData="tableData" :total="page.total" :viewFunc="handleView"
          :delFunc="handleDelete" :page-change="changePage" :currentPage="searchParam.pageNum" :editFunc="handleEdit" :refresh="() => {searchParam.pageNum =1; getData()}">
          <template #toolbarBtn>
              <el-button type="warning" :icon="CirclePlusFilled" @click="openDialog(false)">新增</el-button>
          </template>
      </TableCustom>
    </div>
    <el-dialog :title="isEdit ? '编辑' : '新增'" v-model="visible" width="700px" destroy-on-close
        :close-on-click-modal="false" @close="closeDialog">
        <TableEdit :form-data="rowData" :options="options" :edit="isEdit" :add="addData" :update="updateData" />
    </el-dialog>
    <el-dialog title="查看详情" v-model="visible1" width="700px" destroy-on-close>
      <TableDetail :data="viewData"></TableDetail>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { FormOption, FormOptionList } from '@/types/form-option';
import { CirclePlusFilled } from '@element-plus/icons-vue';
import TableSearch from '@/components/table-search.vue';
import TableCustom from '@/components/table-custom.vue';
import TableEdit from '@/components/table-edit.vue';
import TableDetail from '@/components/table-detail.vue';
import { ScenService } from '@/api/service/scenService'
import { decryptPassword } from '@/utils/crypto'

const query = reactive({
  code: null,
  name: null,
});
const searchParam = reactive({
  pageNum: 1,
  pageSize: 10,
  params: query
})
const searchOpt = ref<FormOptionList[]>([
  // {type: 'input', label: '场景编码', prop: 'code' },
  {type: 'input', label: '场景名称', prop: 'name' },
])
// 新增/编辑弹窗相关
let options = ref<FormOption>({
    labelWidth: '100px',
    span: 24,
    list: [
        // { type: 'input', label: '图标', prop: 'icon', required: true },
        // { type: 'input', label: '场景编码', prop: 'code', required: true },
        { type: 'input', label: '场景名称', prop: 'name', required: true },
    ]
})
const handleSearch = () => {
  getData();
  };

const rowData = ref({});
const isEdit = ref(false);
let columns = ref([
  { type: 'index', label: '序号', width: 120, align: 'center' },
  // { prop: 'icon', label: '图标' },
  // { prop: 'code', label: '场景编码' ,span:24},
  { prop: 'name', label: '场景名称' ,span:24},
  { prop: 'operator', label: '操作', width: 280 },
])
const page = reactive({
    index: 1,
    size: 10,
    total: 0,
})
const tableData = ref([]);
const visible = ref(false)
const openDialog = (val: boolean) => {
  isEdit.value = val
  visible.value = true
};
// 查看详情弹窗相关
const visible1 = ref(false);
const viewData = ref({
    row: {},
    list: []
});
const handleView = (row) => {
    viewData.value.row = { ...row }
    viewData.value.list = columns.value.filter((_,i) => i !=0 && i!= columns.value.length -1);
    visible1.value = true
};
const handleDelete = async (row) => {
  console.log('del',row.id)
    const res = await ScenService.delete(row.id)
    ElMessage.success('删除成功');
    getData();
};
const handleEdit = (row) => {
    rowData.value = { ...row };
    openDialog(true)
};
const changePage = (val: number) => {
    searchParam.pageNum = val;
    getData();
};
const getData = async ()=> {
  const res = await ScenService.page(searchParam)
  tableData.value = res.list;
  page.total = res.total;
}
getData();

const addData = async (val: any) => {
  console.log('addData',val)
  const result = await ScenService.create(val)
    closeDialog();
    getData();
};
const updateData = async (val: any) => {
  const result = await ScenService.update(val)
    closeDialog();
    getData();
};
const closeDialog = () => {
    visible.value = false;
    isEdit.value = false;
};
</script>